package com.shujia.java.day20.demo5;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
* @Description: TCPSocket 客户端向服务端发送 两个数字 服务器给客户端返回一个结果
* @Author: 王火火
* @Date: 2019/8/23 15:14
*/ 

public class TCPSocket {
    public static void main(String[] args) throws Exception{
        //创建Socket
        Socket socket = new Socket("127.0.0.1",9000);
        //创建流
        OutputStream os = socket.getOutputStream();
        byte[] bytes;
        bytes="1".getBytes();
        System.out.println("第一个值为:"+new String(bytes,0,bytes.length));
        //传输两个数据
        os.write(bytes,0,bytes.length);
        //用10把原来的1替换掉 形成一次传递一个数字
        bytes="10".getBytes();
        os.write(bytes,0,bytes.length);
        System.out.println("第二个值为:"+new String(bytes,0,bytes.length));

        //接收服务器端传递的值
        //通过Socket获取getInputStream
        InputStream is = socket.getInputStream();
        int len=is.read(bytes,0,bytes.length);
        System.out.println("结果为:"+new String(bytes,0,len));


    }
}
